package com.meirimoney.task;

import java.io.IOException;
import java.net.URLEncoder;

import com.meiricai.sdk.utility.Common;
import com.meirimoney.app.BaseExchange;
import com.meirimoney.sdk.models.AccessToken;
import com.meirimoney.service.MeiRiMoneyClient;

import android.content.Context;
import android.os.AsyncTask;

/**
 * 兑换任务
 * 
 * @author gewei
 * 
 */
public class ExchangeTask extends AsyncTask<Object, Integer, Boolean>
{

	public ExchangeTask(BaseExchange exchange)
	{
		this.exchange = exchange;
	}

	@Override
	protected Boolean doInBackground(Object... params)
	{
		String commodityId = String.valueOf(params[0]);
		String count = String.valueOf(params[1]);
		String shipAddress = String.valueOf(params[2]);
		AccessToken at = (AccessToken) params[3];
		try
		{
			MeiRiMoneyClient.Instance.exchangeCommodity(commodityId,
					Integer.valueOf(count), shipAddress, at);
		} catch (NumberFormatException e)
		{
			return false;
		} catch (IOException e)
		{
			return false;
		}
		return true;
	}

	@Override
	protected void onPostExecute(Boolean result)
	{
		if (result)
		{
			this.exchange.redirect();
		} 
		else
		{
			Common.networkWarn(this.exchange);
		}
	}

	private BaseExchange exchange;
}
